package com.ehome.poct.core.dao.mybatis;

import cn.hutool.core.util.StrUtil;
import com.ehome.poct.core.api.dto.patient.PatientMsgDTO;
import com.ehome.poct.core.api.dto.check.BaseMsgDTO;
import com.ehome.poct.core.api.dto.check.OnlineRecordDTO;
import com.ehome.poct.core.api.dto.check.OnlineStartedEvent;
import com.ehome.poct.core.api.vo.patient.PatientDetailVO;
import com.ehome.poct.core.dao.rabbitmq.MqttGateway;
import com.ehome.poct.core.repository.CheckPublisher;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author gyl
 * @since 2022/3/7 10:26
 **/
@Component
@AllArgsConstructor
public class CheckPublisherImpl implements CheckPublisher {

    private final MqttGateway mqttGateway;
  //  private final TestDeviceMapper deviceMapper;
    private final ObjectMapper objectMapper;


    @SneakyThrows
    @Override
    public void publishOnlineEvent(OnlineStartedEvent event) {
        // 消息数据
        var data = objectMapper.writeValueAsString(toOnlineRecordDTO(event));
        // 主题
        var topic = "palmpoct_todo." + event.getClinicId();
        // 对于金标仪4.0以上的设备，需要发送新的消息体和新主题
        if ( StrUtil.isNotBlank(event.getDeviceVersion())
                && Integer.valueOf(event.getDeviceVersion().substring(0,1)) >= 4) {
            data = objectMapper.writeValueAsString(toNewRecordEvent(event));
            topic = "ehome_clinic_mqtt_msg_" + event.getClinicId();
        }
        mqttGateway.sendToMqtt(data, topic);
    }


    private OnlineRecordDTO toOnlineRecordDTO(OnlineStartedEvent event) {
        return new OnlineRecordDTO().setRecordId(event.getRecordId()).setCreateTime(event.getCreateTime())
                .setJbyStatus(event.getPoctProcess().getStatus().getType()).setJbyFinishTime(event.getPoctProcess().getFinishTime())
                .setXcgStatus(event.getBloodProcess().getStatus().getType()).setXcgFinishTime(event.getPoctProcess().getFinishTime())
                .setPatient(toPatientMsgVO(event));
    }

    private PatientDetailVO toPatientMsgVO(OnlineStartedEvent event){
        final var detail = event.getDetail();
        return new PatientDetailVO().setPatientId(event.getPatientId()).setUserAge(detail.getAge().getYear())
                .setUserMonth(detail.getAge().getMonth()).setUserName(detail.getName())
                .setMobilePhone(detail.getPhone()).setUserSex(detail.getSex().getValue()).setClinicId(event.getClinicId());
    }

    private PatientMsgDTO toPatientMsgDTO(OnlineStartedEvent event) {
        final var detail = event.getDetail();
        return new PatientMsgDTO().setPatientId(event.getPatientId()).setAge(detail.getAge().getYear())
                .setMonth(detail.getAge().getMonth()).setName(detail.getName())
                .setPhone(detail.getPhone()).setSex(detail.getSex().getValue());
    }

    private BaseMsgDTO<OnlineRecordDTO> toNewRecordEvent(OnlineStartedEvent event) {
        final var msg =
                new BaseMsgDTO<OnlineRecordDTO>(51, "联测消息", LocalDateTime.now(), "com.ehome.health.gold", "血常规");
        msg.setData(toOnlineRecordDTO(event));
        return msg;
    }
}
